<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import PageContainer from '@/components/PageContainer.vue'
import { artGetListService, artDelService } from '@/api/article'
import { ElMessageBox } from 'element-plus'
import ChannelSelect from './components/ChannelSelect.vue'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
const loading = ref(false)
const articleList = ref([])
const total = ref(0)
// const artType = ref()
// const channelList = ref([])
// const getChannelList = async () => {
//   loading.value = true
//   const res = await artGetChannelsService()
//   channelList.value = res.data.data
//   loading.value = false
// }
// getChannelList()

const params = ref({
  pagenum: 1,
  pagesize: 2,
  cate_id: '',
  state: ''
})

const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  console.log(res)
  total.value = res.data.total
  loading.value = false
}
getArticleList()
const onSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const onCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
const articleEditRef = ref()
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
  console.log(row)
}
const onAddArticle = () => {
  articleEditRef.value.open({})
}
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
  console.log(row)
}
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <PageContainer title="文章管理">
    <!--  表单区域 -->
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <template #body1>
      <el-form inline>
        <el-form-item label="文章分类：" label-width="100px">
          <ChannelSelect v-model="params.cate_id"></ChannelSelect>
        </el-form-item>
        <el-form-item label="发布状态：">
          <el-select v-model="params.state" style="width: 240px">
            <!-- 后台用中文存储 -->
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">搜索</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!--  表格区域 -->
      <el-table
        v-loading="loading"
        :data="articleList"
        height="250"
        style="width: 100%"
      >
        <el-table-column prop="title" label="文章标题">
          <template #default="{ row }">
            <el-link type="primary" :underline="false">{{ row.title }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="cate_name" label="分类"> </el-table-column>
        <el-table-column label="发布时间">
          <template #default="{ row }">
            {{ formatTime(row.pub_date) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态"> </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              @click="onEditArticle(row)"
              size="large"
              :icon="Edit"
              circle
              plain
              type="primary"
            ></el-button>

            <el-button
              @click="onDeleteArticle(row)"
              size="large"
              :icon="Delete"
              circle
              plain
              type="danger"
            ></el-button>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="没有数据" />
        </template>
      </el-table>
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 3, 4, 5, 10]"
        layout="jumper, total, sizes, prev, pager, next"
        background
        :total="total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
        style="margin-top: 20px; justify-content: flex-end"
      />
      <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
    </template>
  </PageContainer>
</template>
